#include "ValueObject.h"

#include "RangeObject.h"

OBJECT_DEFINE_START (ValueObject, value_object)

void
value_object_init (ValueObject* obj)
{
        object_node_init (OBJECT_NODE (obj));
        object_set_destory_func (obj, value_object_destory);
        obj->date   = NULL;
        obj->number = NULL;
        obj->text   = NULL;
}

void
value_object_destruction (ValueObject* obj)
{
        object_unref (obj->date);
        object_unref (obj->number);
        object_unref (obj->text);
        object_node_destruction (OBJECT_NODE (obj));
}

int
value_object_load_as_type (ValueObject*             obj,
                           ValueType                type,
                           ObjectStringApplication* str)
{
        int state = 0;
        if (!obj || !str) {
                OBJECT_ERROR_TOOL (ValueObject,
                                   load_as_type,
                                   "参数载入出错！ValueObject: %p, "
                                   "ObjectStringApplication: %p\n",
                                   obj,
                                   str);
                return 1;
        }

        switch (type) {
        case VALUE_TYPE_DATE:
                obj->date = date_object_new ();
                state     = date_object_analysis_date (obj->date, str);
                break;
        case VALUE_TYPE_NUMBER:
        case VALUE_TYPE_RANGE:
                obj->number = NUMBER_OBJECT (range_object_new ());
                state = range_object_analysis_range (RANGE_OBJECT (obj->number),
                                                     str);
                break;
        case VALUE_TYPE_TEXT:
                obj->text = object_string_new_with (
                        object_string_get_string (OBJECT_STRING (str)));
                break;
        default:
                /* VALUE_TYPE_ERROR */
                OBJECT_ERROR_TOOL (
                        ValueObject,
                        load_as_type,
                        "Value "
                        "值类型有误(\033[01;31mVALUE_TYPE_ERROR\033[0m)"
                        "，无法被载入。原始值: '%s'\n",
                        object_string_get_string (OBJECT_STRING (str)));
                state = 1;
                break;
        }

        if (state == 1) {
                OBJECT_ERROR_TOOL (
                        ValueObject,
                        load_as_type,
                        "Value 值载入出错！原始 Value: '%s'\n",
                        object_string_get_string (OBJECT_STRING (str)));
                return 1;
        } else {
                return 0;
        }
}

bool
value_object_is_contain_number (ValueObject* head, int number)
{
        if (!head) {
                OBJECT_WARNING_TOOL (ValueObject,
                                     is_contain_number,
                                     "对象为空！\n");
                return false;
        }
        if (!head->number) {
                OBJECT_WARNING_TOOL (ValueObject,
                                     is_contain_number,
                                     "非数字不能与数字比较。\n");
                return false;
        }
        while (head) {
                if (RANGE_OBJECT (head->number)->upper_range == 0) {
                        if (head->number->number == number)
                                return true;
                } else {
                        if (range_object_is_number_in_range (
                                    RANGE_OBJECT (head->number),
                                    number))
                                return true;
                }
                head = VALUE_OBJECT (object_node_get_next (OBJECT_NODE (head)));
        }
        return false;
}
